package com.example.noval.ui.adapter

import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.ImageView
import android.widget.TextView
import androidx.recyclerview.widget.RecyclerView
import com.bumptech.glide.Glide
import com.example.noval.R
import com.example.noval.db.mode.SearchResp

/**
 * @Description
 * @Author zzl
 * @Date 2024/3/26
 */
class SearchRecyclerAdapter(private val dataList:List<SearchResp>): RecyclerView.Adapter<SearchRecyclerAdapter.MyViewHolder>() {


    private var itemClickListener: OnItemClickListener? = null


    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): MyViewHolder {

        val view = LayoutInflater.from(parent.context)
            .inflate(R.layout.item_search_fragment, parent, false)
       
        return MyViewHolder(view)

    }

    override fun getItemCount(): Int {
        return dataList.size
    }

    override fun onBindViewHolder(holder: MyViewHolder, position: Int) {
        val context = holder.image.context
        Glide.with(context)
            .load(dataList[position].cover)
            .into(holder.image)
        
        holder.title.text=dataList[position].title
        holder.author.text=dataList[position].author
        holder.chapter.text=dataList[position].chapterNum
        holder.description.text=dataList[position].description
        
        holder.itemView.setOnClickListener(View.OnClickListener(){
            
            itemClickListener?.onItemClick(position)
            
        })
    }
    
    public fun setOnItemClick(itemClickListener: OnItemClickListener){
        this.itemClickListener=itemClickListener
    }
    
    
    public interface OnItemClickListener{
        fun onItemClick(position: Int)
    }
    
   inner class MyViewHolder(view:View): RecyclerView.ViewHolder(view) {

       val image:ImageView=view.findViewById(R.id.image)
       val title:TextView=view.findViewById(R.id.title)
       val author:TextView=view.findViewById(R.id.author)
       val chapter:TextView=view.findViewById(R.id.chapter)
       val description:TextView=view.findViewById(R.id.description)
    }
}